/*
    VR-TOOLBOX
    Copyright (C) 2013 Nicolas GUILLIER 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package com.ngu.headingcalc;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.FloatMath;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

	private int windDir = 0;
	private float windSpeed = 10F;
	private int heading = 0;
	private int target = 0;
	
	private WindPolar polar = WindPolar.getVendeeGlobeWithNoOptionPolar();
	private PotarManager roseMngr = null;
	private PolarPlotView polarView = null;
	
	private SharedPreferences prefs;	
	public DecimalFormat speedFormat;
	public DecimalFormat directionFormat;

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.activity_main, menu);
//		return true;
//	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		// init text formats
		speedFormat = new DecimalFormat("#.##" + getString(R.string.speedUnitAbbrev));
		directionFormat = new DecimalFormat("#°");
		
		// Init the rose manager
		roseMngr = new PotarManager(this);
		
		// Init the Controls buttons. Register PotarManager as listener 
		RadioGroup controlRadioGrp = (RadioGroup) findViewById(R.id.radioGroup);
		controlRadioGrp.setOnCheckedChangeListener(roseMngr);
		
		// Init Rose view behaviours
		ImageView roseView = (ImageView) findViewById(R.id.roseView);
		roseView.setOnTouchListener(roseMngr);
		
		// Init the polar 
		polarView = (PolarPlotView) findViewById(R.id.polarChartView);
		polarView.setWindPolar(polar);
	}
	
	@Override
	public void onStart() {	
		super.onStart();
		// retrieved saved values
		prefs = getPreferences(Activity.MODE_PRIVATE);
        windDir = prefs.getInt("windDir", windDir);
        windSpeed = prefs.getFloat("windSpeed", windSpeed);
        heading = prefs.getInt("heading", heading);
        target = prefs.getInt("target", target);


		// Set initial controlled values
		updateControledValues();
		
		// Compute initial results
		computeValues();
		
		// Set the correct angles of roseview using controled values
		roseMngr.initPotar();

		// Init the polar 
		polarView = (PolarPlotView) findViewById(R.id.polarChartView);
		polarView.setWind(windDir, windSpeed, target);
	}
	

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		
		SharedPreferences.Editor ed = prefs.edit();
        ed.putInt("windDir", windDir);
        ed.putFloat("windSpeed", windSpeed);
        ed.putInt("heading", heading);
        ed.putInt("target", target);
        ed.commit();
	}
	

	private void updateControledValues()
	{
		RadioButton windDirBtn = (RadioButton) findViewById(R.id.windDirRadio);
		RadioButton windForceBtn = (RadioButton) findViewById(R.id.windForceRadio);
		RadioButton headingBtn = (RadioButton) findViewById(R.id.headingRadio);
		RadioButton targetBtn = (RadioButton) findViewById(R.id.targetRadio);

		String windDirValue;
		windDirValue = directionFormat.format(windDir);
		if (windDir > 180) {
			windDirValue += "/" + directionFormat.format(windDir-360); 
		}
		windDirBtn.setText(getString(R.string.windDir) + getString(R.string.equalSymbol) + windDirValue);        	

		windForceBtn.setText(getString(R.string.windForce) + getString(R.string.equalSymbol) + speedFormat.format(windSpeed));

		headingBtn.setText(getString(R.string.heading) + getString(R.string.equalSymbol) + directionFormat.format(heading));

		targetBtn.setText(getString(R.string.target) + getString(R.string.equalSymbol) + directionFormat.format(target));


	}

	private void computeValues() {
		float speed;
		int sail;
		int twa;
		int vmcAngle;
		float vmc;
		//		ImageView headingImg = (ImageView) findViewById(R.id.headingView);		
		//    	ImageView targetImg = (ImageView) findViewById(R.id.targetView);
		//    	ImageView windDirImg = (ImageView) findViewById(R.id.windView);

		
		twa = getAngleDiff(heading, windDir);
		
		vmcAngle = getAngleDiff(heading, target);
		
		speed = polar.getBoatSpeed(twa, windSpeed);
		sail = polar.getBestSail(twa, windSpeed);
		vmc = speed * FloatMath.cos((float) Math.toRadians(vmcAngle));

		TextView speedText = (TextView) findViewById(R.id.speedLabel);
		TextView twaText = (TextView) findViewById(R.id.twaLabel);
		TextView vmcText = (TextView) findViewById(R.id.vmcLabel);	

		speedText.setText(getString(R.string.speed) + getString(R.string.equalSymbol) + speedFormat.format(speed) + " / " + Sail.values()[sail-1].toString());
		twaText.setText(getString(R.string.twa) + getString(R.string.equalSymbol) + directionFormat.format(twa));

		vmcText.setText(getString(R.string.vmc) + getString(R.string.equalSymbol) + speedFormat.format(vmc));		
	}
	
	public static final int getAngleDiff(int a, int b) {
		int diff = Math.abs(a - b);
		return Math.abs( diff - 360 * (int) Math.floor(diff / 181) );
	}
	
	public void redrawPolar()
	{
		// Lock the canvas to update
		if (polarView != null) {
			polarView.setWind(windDir, windSpeed, target);
		}
		
//		ImageView roseView = (ImageView) findViewById(R.id.roseView);
//		Log.d("HeadCalc", "RoseView: MeasuredWidth=" + roseView.getMeasuredWidth() + ", MeasuredHeight=" + roseView.getMeasuredHeight());
	}
	

	/**
	 * @return the windDir
	 */
	public int getWindDir() {
		return windDir;
	}

	/**
	 * @return the windSpeed
	 */
	public float getWindSpeed() {
		return windSpeed;
	}

	/**
	 * @return the heading
	 */
	public int getHeading() {
		return heading;
	}

	/**
	 * @return the target
	 */
	public int getTarget() {
		return target;
	}


	public void setWindDir(int windDir) {
		this.windDir = windDir;
		updateControledValues();
		computeValues();
	}

	public void setWindSpeed(float windSpeed) {
		this.windSpeed = windSpeed;
		updateControledValues();
		computeValues();
	}

	public void setHeading(int heading) {
		this.heading = heading;
		updateControledValues();
		computeValues();
	}

	public void setTarget(int target) {
		this.target = target;
		updateControledValues();
		computeValues();
	}
	
	public enum Sail {
		Foc,
		Spi;		
	}

}




